package com.codefish.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codefish.entity.InMoneyType;
import com.codefish.entity.SysUserTokenEntity;
import com.codefish.service.InMoneyTypeService;
import com.codefish.util.Assert;
import com.codefish.util.JWTService;
import com.codefish.util.R;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;


/**
 * 收入类型
 *
 * @author codefish
 * @email cool_fish@aliyun.com
 * @date 2021-07-07 18:00:20
 */
@RestController
@RequestMapping("/inMoneyType")
public class InMoneyTypeController {

    @Autowired
    private InMoneyTypeService inMoneyTypeService;

    /**
     * 新增单个
     *
     * @param token
     * @param inMoneyType
     * @return
     */
    @PostMapping("/saveOne")
    public R saveOne(@RequestHeader("Authentication") String token
            , @RequestBody InMoneyType inMoneyType) {
        Assert.isNull(inMoneyType.getTypeName(), "typeName不可为空");
        Assert.isNull(inMoneyType.getMoneyAccountId(), "moneyAccount不可为空");
        SysUserTokenEntity user = JWTService.getUser(token);
        Assert.isNull(user, "当前用户不存在");
        Assert.isNull(user.getFamilyId(),"familyId不可为空");

        // set createId / familyId
        inMoneyType.setCreateId(user.getId());
        inMoneyType.setFamilyId(user.getFamilyId());

        int id = inMoneyTypeService.saveOne(inMoneyType);

        if (id == -1) {
            return R.error("新增失败");
        }

        return R.ok().put("id", id);
    }


    /**
     * 修改单个
     *
     * @param token
     * @param inMoneyType
     * @return
     */
    @PostMapping("/updateOne")
    public R updateOne(@RequestHeader("Authentication") String token
            , @RequestBody InMoneyType inMoneyType) {
        Assert.isNull(inMoneyType.getId(), "id不可为空");
        Assert.isNull(inMoneyType.getTypeName(), "typeName不可为空");
        Assert.isNull(inMoneyType.getMoneyAccountId(), "moneyAccount不可为空");
        SysUserTokenEntity user = JWTService.getUser(token);
        Assert.isNull(user, "当前用户不存在");
        Assert.isNull(user.getFamilyId(),"familyId不可为空");

        inMoneyType.setUpdateId(user.getId());
        inMoneyType.setUpdateTime(LocalDateTime.now());

        boolean updateFlag = inMoneyTypeService.updateOne(inMoneyType);

        if (!updateFlag) {
            return R.error("修改失败");
        }

        return R.ok("修改成功");
    }

    /**
     * 删除单个
     * @param id
     * @return
     */
        @GetMapping("/deleteOne")
    public R deleteOne(@RequestParam("id")Integer id){

        boolean flag = inMoneyTypeService.deleteOne(id);
        if (!flag){
            return R.error("删除失败");
        }

        return R.ok("删除成功");
    }


    /**
     * 条件分页查询
     * @param token
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getPageByParams")
    public R getPageByParams(@RequestHeader("Authentication") String token
            , @RequestParam("pageNo") Integer pageNo
            , @RequestParam("pageSize") Integer pageSize) {

        Assert.isNull(pageNo,"pageNo不为空");
        Assert.isNull(pageSize,"pageSize不为空");
        SysUserTokenEntity user = JWTService.getUser(token);
        Assert.isNull(user.getId(),"当前用户无效");

        IPage<InMoneyType> retPage = inMoneyTypeService.pagePageByParams(user.getId(),pageNo,pageSize);

        return R.ok().put("data",retPage);
    }

    /**
     * 查询所有
     * @param token token
     * @return
     */
    @GetMapping("/getAll")
    public R getAll(@RequestHeader("Authentication")String token){

        SysUserTokenEntity user = JWTService.getUser(token);
        Assert.isNull(user.getId(),"当前用户无效");
        Assert.isNull(user.getFamilyId(),"familyId无效");

        List<InMoneyType> retList = inMoneyTypeService.getAll(user.getFamilyId());

        if (Collections.isEmpty(retList)){
            return R.error("收入类型为空，请前去添加").put("data", java.util.Collections.emptyList());
        }

        return R.ok("查询成功").put("data",retList);
    }
}
